package org.zb.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.util.Log;
import org.zb.object.MovingObject;
import org.zb.object.PhysicalObject;
import org.zb.object.Player;

public class ObjectController {

	public static Comparator<PhysicalObject> renderedObjectComparator = new Comparator<PhysicalObject>() {
		@Override
		public int compare(PhysicalObject o1, PhysicalObject o2) {
			return (int)(o1.position.y - o2.position.y);
		}		
	};
	
	public List<PhysicalObject> objects = new ArrayList<PhysicalObject>();

	public Player player = new Player();
	
	public void init(GameContainer container) throws SlickException {
		objects.add(player);
		
		for(PhysicalObject object: objects)
			object.init();
	}
	
	public void render(GameContainer container, Graphics g) throws SlickException {
		int offsetX = GameController.getOffsetX();
		int offsetY = GameController.getOffsetY();
		
		for(PhysicalObject object: objects)
			object.render(container, g, -offsetX, -offsetY);
	}

	public void update(GameContainer container, int delta) throws SlickException {
		for(PhysicalObject object: objects)
			object.update(container, delta);
		
		Collections.sort(objects, renderedObjectComparator);
	}
	
	public boolean containsObjectsNear(MovingObject object, float distance) {
		//Log.debug(" position = " + position.toString() + ", distance = " + objects.get(0).position.distance(position));
		for(PhysicalObject otherObject: objects) {
			if(object == otherObject)
				continue;
			if(otherObject.position.distance(object.positionAfterMovement) < distance)
				return true;
		}
		return false;
	}
}
